﻿namespace OboutInc.Show
{
    using System;
    using System.Collections;
    using System.Web.UI;

    internal class ScriptManagerHelper
    {
        private object AjaxSM;
        private bool haveSM;
        private bool isAjax;
        private Type smType;

        public ScriptManagerHelper(Control control)
        {
            if (control.Page != null)
            {
                this.haveSM = true;
                foreach (DictionaryEntry entry in control.Page.Items)
                {
                    if ((entry.Key.ToString().IndexOf("Microsoft.Web.UI.ScriptManager") >= 0) || (entry.Key.ToString().IndexOf("System.Web.UI.ScriptManager") >= 0))
                    {
                        this.isAjax = true;
                        this.AjaxSM = entry.Value;
                        this.smType = this.AjaxSM.GetType();
                        if (((this.smType.FullName == "AjaxControlToolkit.ToolkitScriptManager") || (this.smType.FullName == "System.Web.UI.AjaxScriptManager")) || (this.smType.FullName == "Telerik.Web.UI.RadScriptManager"))
                        {
                            this.smType = this.smType.BaseType;
                        }
                        break;
                    }
                }
            }
        }

        public void RegisterClientScriptBlock(Control control, string key, string script, bool includeScriptTags)
        {
            if (this.isAjax)
            {
                this.smType.GetMethod("RegisterClientScriptBlock", new Type[] { typeof(Control), typeof(Type), typeof(string), typeof(string), typeof(bool) }).Invoke(this.AjaxSM, new object[] { control, control.GetType(), key, script, includeScriptTags });
            }
            else if (this.haveSM)
            {
                control.Page.ClientScript.RegisterClientScriptBlock(control.GetType(), key, script, includeScriptTags);
            }
        }

        public void RegisterClientScriptInclude(Control control, string key, string url)
        {
            if (this.isAjax)
            {
                this.smType.GetMethod("RegisterClientScriptInclude", new Type[] { typeof(Control), typeof(Type), typeof(string), typeof(string) }).Invoke(this.AjaxSM, new object[] { control, control.GetType(), key, url });
            }
            else if (this.haveSM)
            {
                control.Page.ClientScript.RegisterClientScriptInclude(key, url);
            }
        }

        public void RegisterClientScriptResource(Control control, string url)
        {
            if (this.isAjax)
            {
                this.smType.GetMethod("RegisterClientScriptResource", new Type[] { typeof(Control), typeof(Type), typeof(string) }).Invoke(this.AjaxSM, new object[] { control, control.GetType(), url });
            }
            else if (this.haveSM)
            {
                control.Page.ClientScript.RegisterClientScriptResource(control.GetType(), url);
            }
        }

        public void RegisterHiddenField(Control control, string name, string value)
        {
            if (this.isAjax)
            {
                this.smType.GetMethod("RegisterHiddenField", new Type[] { typeof(Control), typeof(string), typeof(string) }).Invoke(this.AjaxSM, new object[] { control, name, value });
            }
            else if (this.haveSM)
            {
                control.Page.ClientScript.RegisterHiddenField(name, value);
            }
        }

        public void RegisterStartupScript(Control control, string key, string script, bool includeScriptTags)
        {
            if (this.isAjax)
            {
                this.smType.GetMethod("RegisterStartupScript", new Type[] { typeof(Control), typeof(Type), typeof(string), typeof(string), typeof(bool) }).Invoke(this.AjaxSM, new object[] { control, control.GetType(), key, script, includeScriptTags });
            }
            else if (this.haveSM)
            {
                control.Page.ClientScript.RegisterStartupScript(control.GetType(), key, script, includeScriptTags);
            }
        }

        public bool IsAjax
        {
            get
            {
                return this.isAjax;
            }
        }

        public bool IsInAsyncCallback
        {
            get
            {
                return (this.isAjax && bool.Parse(this.smType.GetProperty("IsInAsyncPostBack").GetValue(this.AjaxSM, null).ToString()));
            }
        }
    }
}

